﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class GianHangDAO
    {
        #region 1   -   Queries
        public static GianHangDTO LayGianHangTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            GIAN_HANG temp = myDB.GIAN_HANGs.Single(c => c.ID == id);
            if (temp != null)
                return ChuyenTuGianHangSangGianHangDTO(temp);
            return null;
        }

        public static int LayIDGianHangCuoiCung()
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                int index = (from gh in myDB.GIAN_HANGs
                             select gh.ID).Skip(myDB.GIAN_HANGs.Count() - 1).FirstOrDefault();
                return index;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public static List<GianHangDTO> LayDanhSachGianHang()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listGH = from gh in myDB.GIAN_HANGs
                         select gh;

            List<GianHangDTO> result = new List<GianHangDTO>();
            foreach (GIAN_HANG item in listGH)
            {
                result.Add(ChuyenTuGianHangSangGianHangDTO(item));
            }
            return result;
        }

        public static List<GianHangDTO> LayDanhSachGianHangTheoTinhTrang(bool tinhtrang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listGH = from gh in myDB.GIAN_HANGs
                         where gh.TinhTrang == tinhtrang
                         select gh;

            List<GianHangDTO> result = new List<GianHangDTO>();
            foreach (GIAN_HANG item in listGH)
            {
                result.Add(ChuyenTuGianHangSangGianHangDTO(item));
            }
            return result;
        }

        public static GianHangDTO LayGianHangTheoTenGianHang(string tengianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            GIAN_HANG temp = myDB.GIAN_HANGs.Single(c => c.TenGianHang == tengianhang);
            if (temp != null)
                return ChuyenTuGianHangSangGianHangDTO(temp);
            return null;
        }

        public static bool ThemGianHang(GianHangDTO gianhangDTO)
        {
            GIAN_HANG temp = ChuyenTuGianHangDTOSangGianHang(gianhangDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.GIAN_HANGs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaGianHangTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            GIAN_HANG temp = myDB.GIAN_HANGs.Single(c => c.ID == id);
            try
            {
                myDB.GIAN_HANGs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaGianHangTheoMaChuGianHang(int chugianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            GIAN_HANG temp = myDB.GIAN_HANGs.SingleOrDefault(c => c.ChuGianHang == chugianhang);
            if (temp != null)
            {
                try
                {
                    myDB.GIAN_HANGs.DeleteOnSubmit(temp);
                    myDB.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }

        public static int LayMaGianHangTheoChuGianHang(int chugianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            int magianhang = 0;
            try
            {

                GIAN_HANG temp = myDB.GIAN_HANGs.Single(c => c.ChuGianHang == chugianhang);
                if (temp != null)
                    return magianhang = temp.ID;
            }
            catch (Exception)
            {
                return magianhang;
            }
            return magianhang;
        }

        public static bool CapNhatGianHang(GianHangDTO gianhangDTO)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                GIAN_HANG temp = myDB.GIAN_HANGs.Single(c => c.ID == gianhangDTO.ID);
                temp.ThongTinTrinhBay = gianhangDTO.ThongTinTrinhBay;
                temp.TenGianHang = gianhangDTO.TenGianHang;
                temp.TinhTrang = gianhangDTO.TinhTrang;
                temp.DiaChi = gianhangDTO.DiaChi;
                temp.SoDienThoai = gianhangDTO.SoDienThoai;
                myDB.SubmitChanges();

                return true;
            }
            catch (Exception)
            {
                return false;

            }


        }

        #endregion

        #region 2   -   Function Helper

        public static GianHangDTO ChuyenTuGianHangSangGianHangDTO(GIAN_HANG gianhang)
        {
           GianHangDTO result =  new GianHangDTO(gianhang.TenGianHang, gianhang.ChuGianHang,
                gianhang.TinhTrang.Value, gianhang.ThongTinTrinhBay, gianhang.SoLuongTruyCap.Value,
                gianhang.NgayMoGianHang.Value, gianhang.SoLuongSanPham.Value, gianhang.DoanhSoBanDuoc.Value,
                gianhang.DiaChi, gianhang.SoDienThoai);
           result.ID = gianhang.ID;
           return result;
        }

        public static GIAN_HANG ChuyenTuGianHangDTOSangGianHang(GianHangDTO gianhangDTO)
        {
            GIAN_HANG result = new GIAN_HANG();
            result.ChuGianHang = gianhangDTO.ChuGianHang;
            result.DiaChi = gianhangDTO.DiaChi;
            result.DoanhSoBanDuoc = gianhangDTO.DoanhSoBanDuoc;
            result.ID = gianhangDTO.ID;
            result.NgayMoGianHang = gianhangDTO.NgayMoGianHang;
            result.SoDienThoai = gianhangDTO.SoDienThoai;
            result.SoLuongSanPham = gianhangDTO.SoLuongSanPham;
            result.SoLuongTruyCap = gianhangDTO.SoLuongTruyCap;
            result.TenGianHang = gianhangDTO.TenGianHang;
            result.ThongTinTrinhBay = gianhangDTO.ThongTinTrinhBay;
            result.TinhTrang = gianhangDTO.TinhTrang;
            return result;
        }

        #endregion
    }
}
